﻿Imports Microsoft.TeamFoundation.Client
Imports System.Management.Automation

<Cmdlet(VerbsCommon.Add, TeamSystemNouns.RegisteredServer, ConfirmImpact:=ConfirmImpact.Low, SupportsShouldProcess:=True)> _
Public Class AddRegisteredServerCmdlet
    Inherits Cmdlet

    Public Sub New()
    End Sub

    Private _url As String

    <Parameter(Position:=0, Mandatory:=True, ValueFromPipelineByPropertyName:=True), ValidateNotNullOrEmpty()> _
    Public Property Url() As String
        Get
            Return _url
        End Get
        Set(ByVal value As String)
            _url = value
        End Set
    End Property

    Private _name As String

    <Parameter(Position:=1, ValueFromPipelineByPropertyName:=True), ValidateNotNullOrEmpty()> _
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Protected Overrides Sub ProcessRecord()
        'If a name hasn't been specified then use the host name from the URL.
        If String.IsNullOrEmpty(Name) Then
            Name = (New Uri(Name)).Host
        End If

        If ShouldProcess(Name) Then
            RegisteredServers.AddServer(Name, Url)
        End If
    End Sub
End Class
